# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=scudo-malloc
# tied to llvm version
pkgver=17.0.6
pkgrel=0
pkgdesc="Standalone scudo malloc from compiler-rt"
url="https://llvm.org"
arch="all"
license="Apache-2.0 WITH LLVM-exception"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="linux-headers"
checkdepends="gtest-dev"
subpackages="$pkgname-static $pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/compiler-rt-${pkgver//_/}.src.tar.xz
	0001-Add-makefile.patch
	0001-scudo-Detach-the-hooks-from-Scudo-s-internal-impleme.patch
	"
builddir="$srcdir/compiler-rt-${pkgver//_/}.src/lib/scudo/standalone"

build() {
	local cflags_crc32=
	unset CXXFLAGS
	case "$CARCH" in
		aarch64)
			# this has broken emulation in aarch64 qemu (incl qemu-user),
			# which causes it to abort instantly
			CXXFLAGS="-DSCUDO_DISABLE_TBI"
			cflags_crc32="-march=armv8-a+crc"
			;;
		x86*)	cflags_crc32="-mcrc32"
			;;
	esac

	msg "building libscudo"
	make CXXFLAGS="$CXXFLAGS" CXXFLAGS_crc32="$cflags_crc32" TESTS="$(want_check && echo yes || echo no)"
}

check() {
	make test
}

package() {
	install -Dm755 libscudo.so -t "$pkgdir"/usr/lib/
	install -Dm644 libscudo.a -t "$pkgdir"/usr/lib/
	install -Dm644 include/scudo/interface.h -t "$pkgdir"/usr/include/scudo/
}

sha512sums="
56dc9d304dd4ba8d28179077dd99ca7c820ce26dbc70d33b2ba41769928adc900f7618d3efef756b2fc92e322d2f25dba49be966d6583d2d8f1fce9283a9cdf7  compiler-rt-17.0.6.src.tar.xz
1102a512adde2ef31aafa6011a4293d5fa361aa920f88ac20599aab84711e973315e6f6719631be11007aea869ae700e34ed57c7dc7724e8aacbc5b493632849  0001-Add-makefile.patch
366aff393f2e4d0798e28e504f1d06d7a9fd56f3e4093e3057129a69a2ec9f0acbb789138ab52a006eab4f1f2008970fe283218c75f562076475f913e307e963  0001-scudo-Detach-the-hooks-from-Scudo-s-internal-impleme.patch
"
